//
//  HttpBaseAction.m
//  Training
//
//  Created by michael shen on 15/6/17.
//  Copyright (c) 2015年 coolem. All rights reserved.
//

#import "HttpBaseAction.h"
#import "HttpBaseClient.h"
#import "JsonParser.h"

@implementation HttpBaseAction

+ (void)requestWithRequest:(NSString *)request jsonParserSelector:(SEL)selector finish:(HTTPResultBlock)block{
    [self requestWithRequest:request jsonTarget:[JsonParser defaultParser] jsonSelector:selector finish:block];
    
}

+ (void)requestWithRequest:(NSString *)request jsonTarget:(id)target jsonSelector:(SEL)selector finish:(HTTPResultBlock)block{
    [self requestWithRequest:request method:@"POST" jsonTarget:target jsonSelector:selector finish:block];
}

+ (void)requestWithRequest:(NSString *)request method:(NSString *)method jsonTarget:(id)target jsonSelector:(SEL)selector finish:(HTTPResultBlock)block{
    [[HttpBaseClient sharedClient] requestWithRequest:request method:method finish:^(NSURLSessionDataTask *operation,id responseObject , NSError *error) {
        NSLog(@"Request:%@",request);
        [self finishWithOperation:operation repson:responseObject error:error target:target action:selector finish:block];
    }];
}

+ (void)postRequestWithRequest:(NSString *)request withParam:(id)param jsonSelector:(SEL)selector finish:(HTTPResultBlock)block{
    [[HttpBaseClient sharedClient] requestWithRequest:request withParameters:param finish:^(NSURLSessionDataTask *operation,id responseObject , NSError *error) {
        NSLog(@"Request:%@",request);

        [self finishWithOperation:operation repson:responseObject error:error target:[JsonParser defaultParser] action:selector finish:block];

    }];
}

+ (void)putRequestWithRequest:(NSString *)request withParam:(id)param jsonSelector:(SEL)selector finish:(HTTPResultBlock)block{
    [[HttpBaseClient sharedClient] putRequestWithRequest:request withParameters:param finish:^(NSURLSessionDataTask *operation,id responseObject , NSError *error) {
        NSLog(@"Request:%@",request);

        [self finishWithOperation:operation repson:responseObject error:error target:[JsonParser defaultParser] action:selector finish:block];
        
    }];
}

+(void)deleteRequestWithRequest:(NSString *)request withParam:(id)param jsonSelector:(SEL)selector finish:(HTTPResultBlock)block{
    [[HttpBaseClient sharedClient] deleteRequestWithRequest:request withParameters:param finish:^(NSURLSessionDataTask *operation,id responseObject , NSError *error) {
        NSLog(@"Request:%@",request);

        [self finishWithOperation:operation repson:responseObject error:error target:[JsonParser defaultParser] action:selector finish:block];
        
    }];
}

+ (void)finishWithOperation:(NSURLSessionDataTask *)operation repson:(id)responseObj error:(NSError *)error target:(id)target action:(SEL)selector finish:(HTTPResultBlock)block{
    NSLog(@"Response:%@",responseObj);
    NSData *responData = nil;
    if (responseObj != nil) {
        responData = [NSJSONSerialization dataWithJSONObject:responseObj options:NSJSONWritingPrettyPrinted error:nil];
    }
    if (responData == nil) {
        error = [[JsonParser defaultParser] getResultWithResponseData:responData];
        block(@"",error);
        return;
    }
    NSDictionary *data = responseObj;
    if (![data[@"success"] boolValue]||data == nil) {
        error = [[JsonParser defaultParser] getResultWithResponseData:responData];
        block(@"",error);
        return;
    }
    if ([target respondsToSelector:selector]) {
        id result = [target performSelector:selector withObject:responData];
        if (selector == @selector(getResultWithResponseData:) && target == [JsonParser defaultParser]) {
            
            if (result != nil) {
                error = result;
            }
        }else {
            if (result == nil && error == nil) {
                error = [[JsonParser defaultParser] getResultWithResponseData:responData];
            }
        }
        block(result,error);
    }
    else {
        if (error == nil) {
            error = [[JsonParser defaultParser] getResultWithResponseData:responData];
        }
        block(operation,error);
    }
}

@end
